<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ name: 'home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="content">
      <!-- 头部功能 -->
      <template #header>
        <div>
          <el-button type="primary" size="medium" @click="initCategoriesList"
            >添加分类</el-button
          >
        </div>
      </template>
      <!-- 正文内容 -->
      <ZkTable
        :data="types"
        :columns="columns"
        show-index
        :selection-type="false"
        :expand-type="false"
        v-loading="loading"
      >
        <!-- 使用动态分发规则 -->
        <template #state="{ row }">
          <i
            class="tip"
            :class="row.cat_deleted ? 'el-icon-error' : 'el-icon-success'"
          ></i>
        </template>
        <template #level="{ row }">
          <el-tag v-show="row.cat_level == 0" size="mini">一级</el-tag>
          <el-tag v-show="row.cat_level == 1" size="mini" type="success"
            >二级</el-tag
          >
          <el-tag v-show="row.cat_level == 2" size="mini" type="warning"
            >三级</el-tag
          >
        </template>
        <template #btn="{ row }">
          <el-button type="primary" size="mini" icon="el-icon-edit"
          @click="modifyCate(row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeTypeById(row.cat_id)"
            >删除</el-button
          >
        </template>
      </ZkTable>
      <!-- 分页器 -->
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pagesize"
        :total="total"
        :current-page="pagenum"
        @current-change="reloadTypelistPage"
        @size-change="reloadTypelistSize"
      >
      </el-pagination>
    </el-card>
    <!-- 弹层 -->
    <AddCategoriesDialog
      :categoriesVisible.sync="categoriesVisible"
      @handleAddCategories="handleAddCategories"
      :categoriesForm="CategoriesList"
    ></AddCategoriesDialog>

    <ModifyCategoriesPage
    :cateSelectForm.sync="cateSelectForm"
    :modifyCategoriesVisible.sync="modifyCategoriesVisible"
    @handleModifyCate="handleModifyCate"
    ></ModifyCategoriesPage>
  </div>
</template>

<script>
import ZkTable from "vue-table-with-tree-grid";
import AddCategoriesDialog from "@/components/CategoriesPage/AddCategoriesDialog";
import ModifyCategoriesPage from '@/components/CategoriesPage/ModifyCategoriesPage'
export default {
  components: {
    ZkTable,
    AddCategoriesDialog,ModifyCategoriesPage
  },
  data() {
    return {
      columns: [
        // { label:"序号" },
        { label: "分类名称", prop: "cat_name" },
        { label: "是否有效", type: "template", template: "state" },
        { label: "等级", type: "template", template: "level" },
        { label: "操作", type: "template", template: "btn" },
      ],
      // =====
      loading: false,
      total: 0,
      pagesize: 5,
      pagenum: 1,
      query: "",
      types: [],
      //   ====
      categoriesVisible: false,
      modifyCategoriesVisible:false,
      CategoriesList:[],
      cateSelectForm:{}
    };
  },
  methods: {
    reloadTypelistPage(newPageNum) {
      // console.log(this.pagenum,newPageNum);
      this.pagenum = newPageNum;
      this.loadTypeListPage();
    },
    reloadTypelistSize(newPageSize) {
      // console.log(this.pagesize,newPageSize);
      this.pagenum = 1; // 每页条数发生变化，表示分页数量也将发生变化，所以需要重置页面访问
      this.pagesize = newPageSize;
      this.loadTypeListPage();
    },
    async loadTypeListPage() {
      try {
        this.loading = true;
        let result = await this.$api.getCategories({
          pagenum: this.pagenum,
          pagesize: this.pagesize,
        });
        console.log("getCategories", result);
        if (result == "error") return;
        this.total = result.total;
        this.pagenum = result.pagenum + 1;
        this.types = result.result;
      } catch (error) {
        console.log("CategoriesView-loadTypeListPage-error:", error);
      } finally {
        this.loading = false;
      }
    },
    async removeTypeById(id) {
      try {
        console.log(id);
        let result = await this.$api.deleteCategoriesByCid(id);
        if (result == "error") return;
        this.$message.success("删除成功");
        let lastFlag = this.total%this.pagesize==1;
        let lastPageFlag = Math.ceil(this.total/this.pagesize)==this.pagenum;
        if(lastFlag&&lastPageFlag&&this.pagenum!=1){
          this.pagenum = this.pagenum - 1;
        }
        this.loadTypeListPage();
      } catch (error) {
        console.log("CategoriesView-removeTypeById-error:", error);
      }
    },
    async initCategoriesList() {
      try {
        // this.CategoriesList = [];
        let result = await this.$api.getCategories({
          type: 2
        });
        if (result == "error") return;
        console.log("getCategoriesDialog",result);
        this.categoriesVisible = true;
        this.CategoriesList = result;
      } catch (error) {
        console.log("级联菜单列表数据加载失败",error);
      }
    },
    handleAddCategories(dialogState) {
      this.categoriesVisible = dialogState;
    },
    handleModifyCate(dialogState){
      this.modifyCategoriesVisible = dialogState;
    },
    modifyCate(row){
      // console.log(row);
      this.cateSelectForm = row;
      this.modifyCategoriesVisible = true;
    }
  },
  created() {
    this.loadTypeListPage();
  },
};
</script>


<style lang="less" scoped>
.content,
.el-pagination {
  margin-top: 20px;
}
.search-input {
  width: 300px;
}
.add-btn {
  margin-left: 20px;
}
.tip {
  font-size: 18px;
}
.tip.el-icon-error {
  color: red;
}
.tip.el-icon-success {
  color: green;
}
</style>